/*
 * userinfo_transfer.c
 *
 *  Created on: 2013-2-6
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "../head/communicate_with_server/userinfo_transfer.h"

extern int socketfd_login;
extern int socketfd_userinfo;
static struct userinfo user;

//输出用户信息
void output_userinfo() {
	printf("username: %s\n", user.username);
	printf("password: %s\n", user.password);
	printf("name: %s\n", user.name);
	printf("gender: %s\n", user.gender);
	printf("birthday: %s\n", user.birthday);
	printf("country: %s\n", user.country);
	printf("creditType: %s\n", user.creditType);
	printf("creditID: %s\n", user.creditID);
	printf("mobilephoneNum: %s\n", user.mobilephoneNum);
	printf("fixedphoneNum: %s\n", user.fixedphoneNum);
	printf("email: %s\n", user.email);
	printf("address: %s\n", user.address);
	printf("postcode: %s\n", user.postcode);
	printf("type: %s\n", user.type);
}

//向服务器发送登录请求并等待回复
bool login(const struct login_request* request) {
	bool result = true;
	int write_return = write(socketfd_login, request, sizeof(*request)); //发送登录请求
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_login, &result, sizeof(result)); //读取登录结果
	if (read_return == -1) {
		perror("userinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) { //登录成功
		memset(&user, 0, sizeof(user));
		read_return = read(socketfd_login, &user, sizeof(user)); //继续读取用户信息
		if (read_return == -1) {
			perror("userinfo_transfer.c: read error!\n");
			exit(EXIT_FAILURE);
		}
	}

	return result;
}

//向服务器发送编辑个人资料请求并等待回复
bool edit_personial_info(const struct userinfo_request *request) {
	bool result = true;
	struct request_type type;
	type.type = TYPE_EDIT_PERSONAL_INFO;
	int write_return = write(socketfd_userinfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_userinfo, request, sizeof(*request)); //发送修改后的个人资料
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_userinfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("userinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) { //修改个人资料成功，更新客户端保存的用户信息
		char username[USERNAME_LEN];
		char password[PASSWORD_LEN];
		memset(username, 0, sizeof(username));
		memset(password, 0, sizeof(password));
		int username_len = g_utf8_strlen(user.username, USERNAME_LEN);
		g_utf8_strncpy(username, user.username, username_len);
		int password_len = g_utf8_strlen(user.password, PASSWORD_LEN);
		g_utf8_strncpy(password, user.password, password_len);

		memset(&user, 0, sizeof(user));
		g_utf8_strncpy(user.username, username, username_len);
		g_utf8_strncpy(user.password, password, password_len);

		int name_len = g_utf8_strlen(request->user.name, NAME_LEN);
		g_utf8_strncpy(user.name, request->user.name, name_len);

		int gender_len = g_utf8_strlen(request->user.gender, GENDER_LEN);
		g_utf8_strncpy(user.gender, request->user.gender, gender_len);

		int birthday_len = g_utf8_strlen(request->user.birthday, BIRTHDAY_LEN);
		g_utf8_strncpy(user.birthday, request->user.birthday, birthday_len);

		int country_len = g_utf8_strlen(request->user.country, COUNTRY_LEN);
		g_utf8_strncpy(user.country, request->user.country, country_len);

		int credit_type_len = g_utf8_strlen(request->user.creditType,
				CREDIT_TYPE_LEN);
		g_utf8_strncpy(user.creditType, request->user.creditType,
				credit_type_len);

		int creditID_len = g_utf8_strlen(request->user.creditID, CREDIT_ID_LEN);
		g_utf8_strncpy(user.creditID, request->user.creditID, creditID_len);

		int mobilephone_num_len = g_utf8_strlen(request->user.mobilephoneNum,
				MOBILE_PHONE_NUM_LEN);
		g_utf8_strncpy(user.mobilephoneNum, request->user.mobilephoneNum,
				mobilephone_num_len);

		int fixedphone_num_len = g_utf8_strlen(request->user.fixedphoneNum,
				FIXED_PHONE_NUM_LEN);
		g_utf8_strncpy(user.fixedphoneNum, request->user.fixedphoneNum,
				fixedphone_num_len);

		int email_len = g_utf8_strlen(request->user.email, EMAIL_LEN);
		g_utf8_strncpy(user.email, request->user.email, email_len);

		int address_len = g_utf8_strlen(request->user.address, ADDRESS_LEN);
		g_utf8_strncpy(user.address, request->user.address, address_len);

		int postcode_len = g_utf8_strlen(request->user.postcode, POSTCODE_LEN);
		g_utf8_strncpy(user.postcode, request->user.postcode, postcode_len);
	}

	output_userinfo();
	return result;
}

//向服务器发送密码修改请求并等待回复
bool change_password(const struct change_pw_request *request) {
	bool result = true;
	struct request_type type;
	type.type = TYPE_CHANGE_PASSWORD;
	int write_return = write(socketfd_userinfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_userinfo, request, sizeof(*request)); //发送修改后的密码
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_userinfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("userinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	if (result) { //密码修改成功，更新客户端保存的用户信息
		memset(user.password, 0, PASSWORD_LEN);
		strncpy(user.password, request->original_pw,
				strlen(request->original_pw));
	}

	return result;
}

//向服务器发送注册新用户的请求并等待回复
bool register_new_user(const struct userinfo_request *request) {
	bool result = true;
	struct request_type type;
	type.type = TYPE_REGISTER;
	int write_return = write(socketfd_userinfo, &type, sizeof(type));
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}
	write_return = write(socketfd_userinfo, request, sizeof(*request)); //发送新用户的信息
	if (write_return == -1) {
		perror("userinfo_transfer.c: write error!\n");
		exit(EXIT_FAILURE);
	}

	int read_return = read(socketfd_userinfo, &result, sizeof(result));
	if (read_return == -1) {
		perror("userinfo_transfer.c: read error!\n");
		exit(EXIT_FAILURE);
	}

	return result;
}

//登录成功并设置用户信息后，返回用户的信息
struct userinfo* get_user_info() {
	return &user;
}
